﻿
SET QUOTED_IDENTIFIER ON
GO

SET ANSI_NULLS ON
GO

IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_CF_DeleteFiles_V2]') AND type in (N'P', N'PC'))
    DROP PROCEDURE [dbo].[sproc_CF_DeleteFiles_V2];
GO

CREATE PROCEDURE [dbo].[sproc_CF_DeleteFiles_V2]
(
    @fileIdXml  xml
)
AS
BEGIN

SET NOCOUNT ON;
SET XACT_ABORT ON;

IF ( @@TRANCOUNT = 0 )
BEGIN
    RAISERROR(N'This sproc must be executed within an active transaction.', 16, 1);
    RETURN -1;
END

/*
 * XML format
 * <files>
 *  <file id="" />
 * </files>
 */

-- Remove the file records from DB, 
-- and return the deleted records so that the managed code can remove the physical files
DELETE F
    OUTPUT DELETED.*
    FROM dbo.UDS_CF_File F INNER JOIN @fileIdXml.nodes(N'/files/file') X(f) ON F.FileID = X.f.value(N'@id', N'int')

END
GO